<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="承运商统一信用代码" prop="carrierUcss">
        <el-input v-model="formData.carrierUcss" placeholder="请输入承运商统一信用代码" />
      </el-form-item>
      <el-form-item label="承运商/车队长名称" prop="carrierName">
        <el-input v-model="formData.carrierName" placeholder="请输入承运商/车队长名称" />
      </el-form-item>
      <el-form-item label="司机名称" prop="driverName">
        <el-input v-model="formData.driverName" placeholder="请输入司机名称" />
      </el-form-item>
      <el-form-item label="司机身份证号" prop="driverIdCardNo">
        <el-input v-model="formData.driverIdCardNo" placeholder="请输入司机身份证号" />
      </el-form-item>
      <el-form-item label="司机性别" prop="driverGender">
        <el-input v-model="formData.driverGender" placeholder="请输入司机性别" />
      </el-form-item>
      <el-form-item label="司机手机号" prop="driverPhone">
        <el-input v-model="formData.driverPhone" placeholder="请输入司机手机号" />
      </el-form-item>
      <el-form-item label="司机类别" prop="driverKind">
        <el-input v-model="formData.driverKind" placeholder="请输入司机类别" />
      </el-form-item>
      <el-form-item label="签发机关" prop="grantOrg">
        <el-input v-model="formData.grantOrg" placeholder="请输入签发机关" />
      </el-form-item>
      <el-form-item label="身份证有效起始日期" prop="icStartDate">
        <el-date-picker
          v-model="formData.icStartDate"
          type="date"
          value-format="x"
          placeholder="选择身份证有效起始日期"
        />
      </el-form-item>
      <el-form-item label="身份证有效截止日期" prop="icEndDate">
        <el-date-picker
          v-model="formData.icEndDate"
          type="date"
          value-format="x"
          placeholder="选择身份证有效截止日期"
        />
      </el-form-item>
      <el-form-item label="身份证正面" prop="idCardFurl">
        <el-input v-model="formData.idCardFurl" placeholder="请输入身份证正面" />
      </el-form-item>
      <el-form-item label="身份证反面" prop="idCardBurl">
        <el-input v-model="formData.idCardBurl" placeholder="请输入身份证反面" />
      </el-form-item>
      <el-form-item label="驾驶证号" prop="driverLicenseNo">
        <el-input v-model="formData.driverLicenseNo" placeholder="请输入驾驶证号" />
      </el-form-item>
      <el-form-item label="准驾类型" prop="approveType">
        <el-select v-model="formData.approveType" placeholder="请选择准驾类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="初次领证时间" prop="firstCertDate">
        <el-date-picker
          v-model="formData.firstCertDate"
          type="date"
          value-format="x"
          placeholder="选择初次领证时间"
        />
      </el-form-item>
      <el-form-item label="发证机关" prop="issueOrg">
        <el-input v-model="formData.issueOrg" placeholder="请输入发证机关" />
      </el-form-item>
      <el-form-item label="驾驶证有效起始日期" prop="dlStartDate">
        <el-date-picker
          v-model="formData.dlStartDate"
          type="date"
          value-format="x"
          placeholder="选择驾驶证有效起始日期"
        />
      </el-form-item>
      <el-form-item label="驾驶证有效终止日期" prop="dlEndDate">
        <el-date-picker
          v-model="formData.dlEndDate"
          type="date"
          value-format="x"
          placeholder="选择驾驶证有效终止日期"
        />
      </el-form-item>
      <el-form-item label="驾驶证正面" prop="licenseFurl">
        <el-input v-model="formData.licenseFurl" placeholder="请输入驾驶证正面" />
      </el-form-item>
      <el-form-item label="驾驶证反面" prop="licenseBurl">
        <el-input v-model="formData.licenseBurl" placeholder="请输入驾驶证反面" />
      </el-form-item>
      <el-form-item label="从业资格证号" prop="certNo">
        <el-input v-model="formData.certNo" placeholder="请输入从业资格证号" />
      </el-form-item>
      <el-form-item label="从业资格证类别" prop="certKind">
        <el-input v-model="formData.certKind" placeholder="请输入从业资格证类别" />
      </el-form-item>
      <el-form-item label="发证机关" prop="certIssueOrg">
        <el-input v-model="formData.certIssueOrg" placeholder="请输入发证机关" />
      </el-form-item>
      <el-form-item label="有效期至" prop="certEndDate">
        <el-date-picker
          v-model="formData.certEndDate"
          type="date"
          value-format="x"
          placeholder="选择有效期至"
        />
      </el-form-item>
      <el-form-item label="从业资格证照" prop="certUrl">
        <el-input v-model="formData.certUrl" placeholder="请输入从业资格证照" />
      </el-form-item>
      <el-form-item label="审核状态 0-待审核 1-审核通过 -1审核失败" prop="authStatus">
        <el-radio-group v-model="formData.authStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="机构ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入机构ID" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DriverApi, DriverVO } from '@/api/base/driver'

/** 司机管理 表单 */
defineOptions({ name: 'DriverForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  carrierUcss: undefined,
  carrierName: undefined,
  driverName: undefined,
  driverIdCardNo: undefined,
  driverGender: undefined,
  driverPhone: undefined,
  driverKind: undefined,
  grantOrg: undefined,
  icStartDate: undefined,
  icEndDate: undefined,
  idCardFurl: undefined,
  idCardBurl: undefined,
  driverLicenseNo: undefined,
  approveType: undefined,
  firstCertDate: undefined,
  issueOrg: undefined,
  dlStartDate: undefined,
  dlEndDate: undefined,
  licenseFurl: undefined,
  licenseBurl: undefined,
  certNo: undefined,
  certKind: undefined,
  certIssueOrg: undefined,
  certEndDate: undefined,
  certUrl: undefined,
  authStatus: undefined,
  deptId: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DriverApi.getDriver(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DriverVO
    if (formType.value === 'create') {
      await DriverApi.createDriver(data)
      message.success(t('common.createSuccess'))
    } else {
      await DriverApi.updateDriver(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    carrierUcss: undefined,
    carrierName: undefined,
    driverName: undefined,
    driverIdCardNo: undefined,
    driverGender: undefined,
    driverPhone: undefined,
    driverKind: undefined,
    grantOrg: undefined,
    icStartDate: undefined,
    icEndDate: undefined,
    idCardFurl: undefined,
    idCardBurl: undefined,
    driverLicenseNo: undefined,
    approveType: undefined,
    firstCertDate: undefined,
    issueOrg: undefined,
    dlStartDate: undefined,
    dlEndDate: undefined,
    licenseFurl: undefined,
    licenseBurl: undefined,
    certNo: undefined,
    certKind: undefined,
    certIssueOrg: undefined,
    certEndDate: undefined,
    certUrl: undefined,
    authStatus: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}
</script>